<template>
  <div class="page-container">
    <div class="page-content">
      <div class="panel-container">
        <div class="panel-content">
          <div
            class="panel-line flex-end"
            style="margin-bottom: 20px"
          >
            <el-button
              type="primary"
              @click="handleAdd"
            >
              + 新增
            </el-button>
          </div>
        </div>
        <!--数据信息-->
        <el-table
          ref="table"
          v-loading="dataListLoading"
          stripe
          :data="dataList"
          :header-cell-style="{ background: '#F5F5F5', color: '#000000' }"
          style="width: 100%"
          row-key="id"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column
            align="left"
            header-align="center"
            label="菜单名称"
            prop="title"
          />
          <el-table-column
            align="center"
            header-align="center"
            label="菜单类型"
            prop="level"
          >
            <template slot-scope="scope">
              {{ getLevel(scope.row.level) }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            header-align="center"
            label="组件"
            prop="name"
          />
          <el-table-column
            align="center"
            header-align="center"
            label="路径"
            prop="path"
          />
          <el-table-column
            align="center"
            header-align="center"
            label="排序"
            prop="sort"
          />
          <el-table-column
            align="center"
            fixed="right"
            header-align="center"
            label="操作"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="handleEdit(scope)"
              >
                编辑
              </el-button>
              <el-button
                type="text"
                class="button-danger"
                @click="handleDelete(scope)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageNumber"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pageTotal"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
        />
      </div>
    </div>

    <!--  新增菜单 -->
    <el-drawer
      :title="current && current.id ? '编辑菜单' : '新增菜单'"
      :visible.sync="showDialogAdd"
      size="50%"
    >
      <DialogAdd
        v-if="showDialogAdd"
        :menu-list="dataList"
        :info="current"
        @close="showDialogAdd = false"
        @success="handleEditSuccess"
      />
    </el-drawer>
  </div>
</template>

<script>
import { apiSysPermissionList, apiSysPermissionDel } from '@/api/system'
import DialogAdd from './components/DialogAdd.vue'

export default {
  name: 'Menu',
  components: { DialogAdd },
  data() {
    return {
      current: null,
      dataList: [],
      pageNumber: 1,
      pageSize: 10,
      pageTotal: 0,
      dataListLoading: false,
      showDialogAdd: false
    }
  },
  activated() {
    this.$refs.table.doLayout()
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = { pageNumber: this.pageNumber, pageSize: this.pageSize }
      apiSysPermissionList(info)
        .then((res) => {
          const { data, total } = res
          this.dataList = data
          this.pageTotal = total
          this.dataListLoading = false
          this.$refs.table.doLayout()
        })
        .catch((e) => {
          this.dataList = []
          this.pageTotal = 0
          this.dataListLoading = false
        })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageNumber = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageNumber = val
      this.getDataList()
    },
    // 编辑
    handleEdit(scope) {
      this.current = scope.row
      this.showDialogAdd = true
    },
    // 添加
    handleAdd() {
      this.current = null
      this.showDialogAdd = true
    },
    // 删除
    handleDelete(scope) {
      window.enterPreventDefault = false
      this.$confirm('确定要删除' + scope.row.title + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          window.enterPreventDefault = true
          this.dataListLoading = true
          apiSysPermissionDel({ ids: [scope.row.id] })
            .then(() => {
              this.dataListLoading = false
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getDataList()
            })
            .catch(() => {
              this.dataListLoading = false
            })
        })
        .catch(() => {
          window.enterPreventDefault = true
        })
    },
    handleEditSuccess() {
      this.showDialogAdd = false
      this.getDataList()
    },
    getLevel(level) {
      if (level === 1) {
        return '目录'
      } else if (level === 2) {
        return '菜单'
      } else if (level === 3) {
        return '按钮'
      }
    }
  }
}
</script>
